﻿using Microsoft.AspNetCore.Mvc;
using Qy.Core;
using Qy.Scenario;
using Qy.Web.Authorize;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Qy.Web.Areas.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class StatisticsController : Base
{
    private readonly LogService _logService;
    private readonly ApplicationContext _applicationContext;
    private readonly NoticeKeyService _noticeKey;
    private readonly UserService _adminUserService;
    private readonly StatisticsService _statisticsService;
    public StatisticsController(
        ApplicationContext applicationContext,
        UserService adminUserService,
        LogService logService,
        NoticeKeyService noticeKeyService,
        StatisticsService statisticsService)
    {
        _applicationContext = applicationContext;
        _adminUserService = adminUserService;
        _logService = logService;
        _noticeKey = noticeKeyService;
        _statisticsService = statisticsService;
    }

    [HttpGet]
    public async Task<IActionResult> WorkspaceAsync(string contentCountMod = "")
    {
        int userId = _applicationContext.ILoginUserInfoMethods.LoginUserId();
        NumCount countAll = await _statisticsService.CountAllAsync();
        List<ContentCount> forUser = await _statisticsService.CountContentForUserAsync(contentCountMod);
        List<ViewLogSql> logList = await _logService.GetListAsync(userId,3);//显示3条
        string[] systemInfo = { CxuuLxx.FrameworkDescription, CxuuLxx.OSDescription, CxuuLxx.Ver };
        return Success(new { logList, forUser, countAll, systemInfo });
    }


    [HttpGet]
    public async Task<IActionResult> CountAsync()
    {
        int userId = _applicationContext.ILoginUserInfoMethods.LoginUserId();
        long notice = await _noticeKey.CountAsync(userId);//统计消息未读
        return Success(new { data = notice });
    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.Statistics_Index)]
    public async Task<IActionResult> ListAsync([FromQuery] StatisticsQuery query)
    {
        List<UserIdAndName> userIdAndName = await _adminUserService.GetNameAsync(0);
        return Success(new { code = 0, data = userIdAndName });
    }
}
